--[[
    游戏内聊天模块
]]--

local blaze = require "blaze"
local gamelib = require "club.threecard.gamelib"
local app = require "app"

local log = blaze.logging.get("chat")

local M = {
    MSG_TYPE_TEXT = 1, -- 纯文本聊天
    MSG_TYPE_PHRASE = 2, -- 短语聊天
    MSG_TYPE_EMOJI = 3, -- 表情聊天

    FACING_RIGHT = 1, -- 聊天气泡在右边
    FACING_LEFT = 2, -- 聊天气泡在左边
}

local activity

local function get_or_create_activity()
    if not activity then
        local config = app.game.running.config.ui.chat
        if config then
            error("TODO:根据配置创建Activity")
        else
            local ChatUI = require "club.threecard.gamelib.UI.Chat.ChatUI"
            activity = ChatUI.new()
            activity:set_layer(app.config.ui.layout.popup.layer - 10)
        end
    end
    return activity
end

function M.init()
    M.unhook = app.router.before_each(function(to, from, next)
        next()
        if to.meta.chat then
            app.coroutine.wait(0.5)
            M.show()
        else
            M.hide()
        end
    end)
end

-- 显示聊天界面
function M.show()
    local activity = get_or_create_activity()
    if not activity then
        return
    end
    activity:load()
    activity:show()
end

-- 隐藏聊天界面
function M.hide()
    if not activity then
        return
    end
    activity:hide()
end

function M.release()
    M.unhook()
    if activity then
        activity:release()
        activity = nil
    end
end

return M